package gov.kqqzyyy.monkey.clinicscreen.adapter;

import android.content.Context;
import android.preference.PreferenceManager;
import android.util.ArrayMap;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import gov.kqqzyyy.monkey.clinicscreen.R;
import gov.kqqzyyy.monkey.clinicscreen.mytools.MarqueeTextView;
import gov.kqqzyyy.monkey.clinicscreen.mytools.MyStringUtil;
import gov.kqqzyyy.monkey.clinicscreen.objects.ClinicPatient;
import gov.kqqzyyy.monkey.clinicscreen.objects.DoctorGroup_New;

/**
 * Created by Monkey on 2018/1/24.
 * 诊区病人适配器
 */
public class ClinicInfoAdapter extends BaseAdapter {
    private final Context mContext;
    private final LayoutInflater inflater;
    private ArrayMap<DoctorGroup_New, List<ClinicPatient>> dataMap;
    private List<DoctorGroup_New> parentList;

    private double itemHeight = 0;

    private int totalDocNum;

    public ClinicInfoAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setDataMap(ArrayMap<DoctorGroup_New, List<ClinicPatient>> dataMap) {
        this.dataMap = dataMap;
        doInitParentList();
        notifyDataSetChanged();

    }

    public ArrayMap<DoctorGroup_New, List<ClinicPatient>> getDataMap() {
        return dataMap;
    }

    private void doInitParentList() {
        Set set = dataMap.keySet();
        parentList = new ArrayList<>();
        totalDocNum = 0;
        for (Object object : set) {
            totalDocNum += ((DoctorGroup_New) object).getList().size();
            parentList.add(((DoctorGroup_New) object));
        }
        Collections.sort(parentList, (arg0, arg1) -> arg0.getSH() - arg1.getSH());
    }

    @Override
    public int getCount() {
        if (dataMap != null) {
            return dataMap.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_clinic_info_v2, parent, false);

            holder.listView = convertView.findViewById(R.id.item_doctor_listview);
            holder.zsbrText = convertView.findViewById(R.id.item_patient_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        holder.linearLayout.setLayoutParams(layoutParams2);
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 3);

        LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 4);

        int onedps = (int) (mContext.getResources().getDisplayMetrics().density);
        layoutParams1.setMargins(onedps, onedps, onedps, 0);
        if (position == parentList.size() - 1) {
            layoutParams3.setMargins(onedps, onedps, onedps, onedps);
        } else {
            layoutParams3.setMargins(onedps, onedps, onedps, 0);
        }
        holder.listView.setLayoutParams(layoutParams1);
        holder.zsbrText.setLayoutParams(layoutParams3);

        List<ClinicPatient> list = dataMap.get(parentList.get(position));
//        if (list != null) {
////            Collections.sort(list, (arg0, arg1) -> arg0.getBRHM().compareTo(arg1.getBRHM()));
//            Collections.sort(list, (arg0, arg1) -> arg0.getSXH() - arg1.getSXH());
//        }
        StringBuilder brxm = new StringBuilder();
        boolean ishide = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("hidename_preference", true);
        for (ClinicPatient patient : Objects.requireNonNull(list)
        ) {
            if (brxm.toString().equals("")) {
                if (ishide) {
                    brxm.append(patient.getBRHM()).append("号").append(MyStringUtil.doHideName(patient.getBRXM()));
                } else {
                    brxm.append(patient.getBRHM()).append("号").append(patient.getBRXM());
                }
            } else {
                if (ishide) {
                    brxm.append("  ").append(patient.getBRHM()).append("号").append(MyStringUtil.doHideName(patient.getBRXM()));
                } else {
                    brxm.append("  ").append(patient.getBRHM()).append("号").append(patient.getBRXM());
                }
            }
            if ("20".equals(patient.getYXJ())) {
                brxm.append("(回诊)");
            }
        }
        holder.zsbrText.setText(brxm.toString());

        int textSize;
        if (totalDocNum > 6) {
            textSize = 27 - totalDocNum;
        } else {
            textSize = 27;
        }

        holder.zsbrText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        DoctorAdapter adapter = new DoctorAdapter(mContext);
        holder.listView.setAdapter(adapter);
        adapter.setTextSize(textSize);
        adapter.setDoctorList(parentList.get(position).getList());

//        for (int i = 0; i < parentList.get(position).getList().size(); i++
//        ) {
//        if (position==0){
//            View itemView = adapter.getView(0, null, holder.listView);
//            itemView.measure(0, 0);
//            itemHeight = itemView.getMeasuredHeight();
//        }

        if (totalDocNum <= 6) {
            itemHeight = 72;
        } else {
            itemHeight = 80 - totalDocNum * 3 - (totalDocNum/7*3);
        }

//        }
        ViewGroup.LayoutParams groupParams = holder.listView.getLayoutParams();
        int count = parentList.get(position).getList().size();
//        groupParams.height = (int) (itemHeight + (holder.listView.getDividerHeight() * (parentList.get(position).getList().size() - 1)));

        groupParams.height = (int) (itemHeight * count + 8 * (count - 1));
        holder.listView.setLayoutParams(groupParams);
        ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, groupParams.height);
        convertView.setLayoutParams(params);

        return convertView;
    }

    private static class ViewHolder {
        private ListView listView;
        //        private TextView ysxmText;
//        private TextView zsmcText;
//        private LinearLayout linearLayout;
        private MarqueeTextView zsbrText;
    }

}
